home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / HDS 3.02 / Exception / exhandle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  1.5 KB  |  95 lines  |  [TEXT/MPS ]

  1. /*    A general-purpose exception-handling system for C                    *
  2.  *    by Jonathan Amsterdam, 1991                                            *
  3.  *    BYTE August 1991, p. 259 ff                                            */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "exhandle.h"
  8.  
  9. /* Alert IDs */
  10.  
  11. #ifdef DBOX
  12. #include <Dialogs.h>
  13. #define UNHANDLE        256
  14. #define EMPTYSTACK        257
  15. #define CORRUPTSTACK    258
  16. #endif
  17.  
  18. except theException;
  19. jmp_buf_rec *cur_rec = NULL;
  20.  
  21. void push_jbr (jbr)
  22. jmp_buf_rec *jbr;
  23. {
  24.     jbr->next = cur_rec;
  25.     jbr->self = jbr;
  26.     cur_rec = jbr;
  27. }
  28.  
  29. void pop_jbr ()
  30. {
  31.     if (cur_rec == NULL) {
  32. #ifdef DBOX
  33.         StopAlert (EMPTYSTACK, nil);
  34. #else
  35.         fprintf (stderr, "Attempt to pop empty exception stack");
  36.         fflush (stderr);
  37. #endif
  38.         exit (1);
  39.     } else {
  40.         cur_rec = cur_rec->next;
  41.     }
  42. }
  43.  
  44. void exraise (ex)
  45. int ex;
  46. {
  47.     jmp_buf_rec *jbr;
  48. #ifdef DBOX
  49.     char buff[256];
  50. #ifdef CODERES
  51.     static unsigned char *blank;
  52.     
  53.     blank = "\p";
  54. #else
  55.     static unsigned char *blank = "\p";
  56. #endif
  57. #endif
  58.  
  59.     if (cur_rec == NULL) {
  60. #ifdef DBOX
  61.         sprintf (buff, "%d", ex);
  62.         ParamText (c2pstr (buff), blank, blank, blank);
  63.         StopAlert (UNHANDLE, nil);
  64. #else
  65.         fprintf (stderr, "Unhandled exception: %d", ex);
  66.         fflush (stderr);
  67. #endif
  68.         exit (2);
  69.     } else {
  70.         theException.code = ex;
  71.         jbr = cur_rec;
  72.         if (jbr->self != jbr) {
  73. #ifdef DBOX
  74.             StopAlert (CORRUPTSTACK, nil);
  75. #else
  76.             fprintf (stderr, "Corrupted exception stack");
  77.             fflush (stderr);
  78. #endif
  79.             exit (2);
  80.         }
  81.         pop_jbr ();
  82.         longjmp (jbr->jb, 1);
  83.     }
  84. }
  85.  
  86. void exraisebuff (except *ex)
  87. {
  88.     theException.buff = ex->buff;
  89.     exraise (ex->code);
  90. }
  91.  
  92. void reraise ()
  93. {
  94.     exraisebuff (&theException);
  95. }